<template>
    <div class="postsection" style="">

        <el-table :data="tableData.filter(data => !search || data.name.toLowerCase().includes(search.toLowerCase()))"
            style="width: 100%" border>
            <el-table-column label="id" prop="id">
            </el-table-column>
            <el-table-column label="标题" prop="title">
            </el-table-column>
            <el-table-column label="建议者" prop="userName">
            </el-table-column>
            <el-table-column label="意见" prop="content">
            </el-table-column>
            <el-table-column label="分区代码" prop="code">
            </el-table-column>
            <el-table-column label="状态" prop="status">
                <template slot-scope="scope">
                    <!-- 根据状态值渲染不同的图标和颜色 -->
                    <i :class="getStatusIcon(scope.row.status)" :style="getStatusStyle(scope.row.status)">
                        {{ getStatusText(scope.row.status) }}
                    </i>
                </template>
            </el-table-column>
            <el-table-column label="创建时间" prop="createTime">
            </el-table-column>

            <el-table-column label="操作" align="center" width="160">
                <template slot-scope="scope">
                    <!-- 回复按钮，只有在状态为未回复时才显示 -->
                    <el-button v-if="scope.row.status === 0" size="mini" @click="handleEdit(scope.$index, scope.row)">
                        回复
                    </el-button>
                    <el-button v-else size="mini" type="success" disabled>
                        已回复
                    </el-button>

                    <!-- 删除按钮 -->
                    <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 弹窗   -->
        <el-dialog title="分区" :visible.sync="dialogFormVisible" width="30%" :close-on-click-modal="false">
            <el-form :model="entity">
                <el-form-item label="分区名字" label-width="150px">
                    <el-input v-model="entity.typeName" autocomplete="off" style="width: 80%"></el-input>
                </el-form-item>
                <el-form-item label="分区代码" label-width="150px">
                    <el-input v-model="entity.typeCode" autocomplete="off" style="width: 80%"></el-input>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="save()">确 定</el-button>
            </div>
        </el-dialog>
        <!-- 分页 -->
        <div class="page" style="margin: 24px auto;display: flex;justify-content: center;" v-show="pageShow">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                :current-page.sync="currentPage" :page-size="pageSize" :page-sizes="[3, 6, 9, 12]"
                layout="total, prev, pager, next" :total="total">
            </el-pagination>
        </div>
    </div>
</template>

<script>
import API from "@/api/request";
export default {
    inject: ['reload'],
    data() {
        return {
            tableData: [],
            search: '',
            //数据总数
            total: 0,
            //当前页
            currentPage: 1,
            //每页数量
            pageSize: 3,
            //控制分页显示
            pageShow: false,
            dialogFormVisible: false,
            entity: {},
        }
    },
    methods: {

        handleEdit(index, row) {
            console.log(index, row);
            this.$prompt('', '', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                inputPattern: /.+/,  // 确保输入内容不为空
                inputErrorMessage: '回复内容不能为空'
            }).then(({ value }) => {
                this.reply(row.id, value);
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '取消输入'
                });
            });


        },
        handleDelete(index, row) {
            console.log(row)
            API.delete("/api/suggestion/" + row.id).then(res => {
                if (res.data.code === 200) {
                    this.$message.success("操作成功!");
                } else {
                    this.$message.error("操作失败!");
                    console.error("API 请求失败", res);
                }
                this.reload();
            }).catch(error => {
                console.error("API 请求错误", error);
            });
        },

        getsuggestions() {
            API.get("/api/suggestion/pageList", {
                params: {
                    pageNum: this.currentPage,
                    pageSize: this.pageSize
                }
            }).then(res => {
                if (res.data.code === 200) {
                    this.tableData = res.data.data.records;
                    this.total = res.data.data.total;
                    this.currentPage = res.data.data.current;
                    if (this.total > 0) {
                        this.pageShow = true;
                    }

                } else {
                    console.error("API 请求失败", res);
                }
            }).catch(error => {
                console.error("API 请求错误", error);
            });
        },
        //回复
        reply(id, content) {
            API.put("/api/suggestion/suggestion", {
                "id": id,
                "reply": content
            }).then(res => {
                console.log(res)
                if (res.data.code == 200) {
                    this.$message({
                        type: 'success',
                        message: '回复成功 '
                    });
                    this.reload();
                }

            })
        },
        //控制页数的函数
        handleSizeChange(val) {
            this.pageSize = val;
            console.log(`每页 ${val} 条`);
            this.getsuggestions();

        },
        // 控制页码的函数
        handleCurrentChange(val) {
            this.currentPage = val;
            console.log("查看当前页" + this.currentPage);
            console.log(`当前 ${val} 页`);
            this.getsuggestions();
        },
        // 根据状态值返回不同的图标
        getStatusIcon(status) {
            switch (status) {
                case 0:
                    return 'el-icon-loading';
                case 1:
                    return 'el-icon-circle-check';
                default:
                    return 'el-icon-question'; // 默认图标
            }
        },
        // 根据状态值返回不同的文本
        getStatusText(status) {
            console.log(status)
            switch (status) {
                case 0:
                    return '未回复';
                case 1:
                    return '已回复';
                default:
                    return '未知';
            }
        },
        // 根据状态值设置不同的文本颜色
        getStatusStyle(status) {
            switch (status) {
                case 0:
                    return { color: 'orange' };
                case 1:
                    return { color: 'red' };
                case 3:
                    return { color: 'green' };
                default:
                    return { color: 'gray' };
            }
        },





    },


    created() {
        this.getsuggestions();
    }
}
</script>
<style>
.postsection {
    padding: 20px;
}
</style>